﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Licenta_Stomatologie.Panels
{
    public partial class AppointmentEditPanel : UserControl
    {
        private int appId = 1;
        private int pacId = 1;
        private int[] pacientIds = new int[1000];

        public AppointmentEditPanel()
        {
            InitializeComponent();
        }

        public AppointmentEditPanel(int appointmentId)
        {
            InitializeComponent();
            appId = appointmentId;
            fillWithPacients();
            getAppointmentDetails();
        }

        private void fillWithPacients()
        {
            ProjectDBEntities db = new ProjectDBEntities();

            IEnumerable<Pacient> pacienti = db.Pacients.Where(p => p.id > 0);

            int i = 0;
            foreach (Pacient pacient in pacienti)
            {
                cboxPacient.Items.Add(pacient.nume + " " + pacient.prenume);
                pacientIds[i] = pacient.id;
                i++;
            }
        }

        private int findIndex(int pid)
        {
            for (int i = 0; i < pacientIds.Length; i++)
            {
                if (pacientIds[i] == pid)
                    return i;
            }
            return -1;
        }

        private void getAppointmentDetails()
        {
            ProjectDBEntities db = new ProjectDBEntities();

            Appointment app = db.Appointments.First(a => a.id == appId);

            dtpAppointmentDate.Value = new DateTime(app.date.Value.Year, app.date.Value.Month, app.date.Value.Day);
            dtpAppointmentTime.Value = new DateTime(1999, 10, 10, app.date.Value.Hour, app.date.Value.Minute, app.date.Value.Second);
            cboxPacient.SelectedIndex = findIndex( (int) app.pacientId );
            cboxAppointmentType.SelectedIndex = (int) app.type;
            tbAppointmentDuration.Text = app.duration.ToString();
            tboxCost.Text = app.cost.ToString();
            cboxAchitat.SelectedIndex = (int) app.achitat;
        }

        private void btnCancelAppointmentCreate_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(Helpers.UserMessages.APPOINTMENT_CREATE_CANCEL, "Confirmation", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                this.Controls.Clear();
                this.Controls.Add(new Panels.Dashboard());
            }
        }

        private void btnCreateAppointment_Click(object sender, EventArgs e)
        {
            ProjectDBEntities db = new ProjectDBEntities();

            Appointment app = db.Appointments.First(a => a.id == appId);

            DateTime when = new DateTime(dtpAppointmentDate.Value.Year, dtpAppointmentDate.Value.Month, dtpAppointmentDate.Value.Day, dtpAppointmentTime.Value.Hour, dtpAppointmentTime.Value.Minute, dtpAppointmentTime.Value.Second);

            app.date = when;
            app.duration = int.Parse(tbAppointmentDuration.Text);
            app.pacientId = pacientIds[cboxPacient.SelectedIndex];
            app.type = cboxAppointmentType.SelectedIndex;
            app.cost = int.Parse(tboxCost.Text);
            app.achitat = cboxAchitat.SelectedIndex;

            db.SaveChanges();

            MessageBox.Show(Helpers.UserMessages.APPOINTMENT_CREATE_SUCCESS);

            this.Controls.Clear();
            this.Controls.Add(new Panels.Dashboard());
        }

        private void btnDeleteAppointment_Click(object sender, EventArgs e)
        {
            ProjectDBEntities db = new ProjectDBEntities();

            Appointment app = db.Appointments.First(a => a.id == appId);

            db.Appointments.Remove(app);

            db.SaveChanges();

            MessageBox.Show("Programarea a fost eliminata cu succes!");

            this.Controls.Clear();
            this.Controls.Add(new Panels.Dashboard());
        }


    }
}
